package com.zj.osm.view.home.home;

import com.zj.osm.App;
import com.zj.osm.entity.db.DaoSession;
import com.zj.osm.entity.db.LineDao;
import com.zj.osm.entity.db.PointDao;
import com.zj.osm.entity.db.ProjectDao;
import com.zj.osm.entity.greendao.Line;
import com.zj.osm.entity.greendao.Point;
import com.zj.osm.entity.greendao.Project;

import java.util.List;

/**
 * 主页的数据处理类
 *
 * @author jiang zhu on 2019/10/26
 */
class HomeActivityModel {

    private DaoSession daoSession = App.getInstances().getDaoSession();


    List<Project> getProjectList(int page) {
        List<Project> projectList = daoSession.getProjectDao().queryBuilder().orderDesc(ProjectDao.Properties.Id).list();
        if (page == 1 && projectList.size() <= 15) {
            return projectList;
        } else
            return daoSession.getProjectDao().queryBuilder().offset(page * 15).orderDesc(ProjectDao.Properties.Id).limit(15).list();
    }

    void deleteProject(Project project) {
        daoSession.getProjectDao().delete(project);
        daoSession.queryBuilder(Point.class).where(PointDao.Properties.ProjectId.eq(project.getId())).buildDelete().executeDeleteWithoutDetachingEntities();
        daoSession.queryBuilder(Line.class).where(LineDao.Properties.ProjectId.eq(project.getId())).buildDelete().executeDeleteWithoutDetachingEntities();
    }
}
